package cn.cs.service.impl;

import cn.cs.client.RestUserCompanyClient;
import cn.cs.common.Constants;
import cn.cs.common.EmptyUtils;
import cn.cs.common.Page;
import cn.cs.pojo.UserCompany;
import cn.cs.service.UserCompanyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class UserCompanyServiceImpl implements UserCompanyService {

    @Resource
    private RestUserCompanyClient userCompanyClient;

    public UserCompany getUserCompanyById(String id) throws Exception {
        return userCompanyClient.getUserCompanyById(id);
    }

    public List<UserCompany> getUserCompanyListByMap(Map<String, Object> param) throws Exception {
        return userCompanyClient.getUserCompanyListByMap(param);
    }

    public Integer getUserCompanyCountByMap(Map<String, Object> param) throws Exception {
        return userCompanyClient.getUserCompanyCountByMap(param);
    }

    public Integer addUserCompany(UserCompany userCompany) throws Exception {
        userCompany.setCreateTime(new Date());
        return userCompanyClient.addUserCompany(userCompany);
    }

    public Integer modifyUserCompany(UserCompany userCompany) throws Exception {
        userCompany.setUpdateTime(new Date());
        return userCompanyClient.modifyUserCompany(userCompany);
    }

    public Integer deleteUserCompanyById(String id) throws Exception {
        return userCompanyClient.deleteUserCompanyById(id);
    }

    public Page<UserCompany> queryUserCompanyPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) throws Exception {
        Integer total = userCompanyClient.getUserCompanyCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<UserCompany> userCompanyList = userCompanyClient.getUserCompanyListByMap(param);
        page.setRows(userCompanyList);
        return page;
    }

    @Override
    public Integer updateAddressTypeId(String id) throws Exception {
        return userCompanyClient.updateCompanyTypeId(id);
    }

}
